package io.github.shenbinglife.springboot2.layui.common.utils;

import javax.persistence.AttributeConverter;
import java.util.Map;

/**
 * JpaConverterMapJson
 *
 * @author shenbing
 * @date 2021/4/12 22:15
 */
public class JpaConverterMapJson  implements AttributeConverter<Map<String, Object>, String> {
    @Override
    public String convertToDatabaseColumn(Map<String, Object> attribute) {
        if (attribute == null) {
            return null;
        }
        return JsonUtils.toJson(attribute);
    }

    @Override
    public Map<String, Object> convertToEntityAttribute(String dbData) {
        if (dbData == null) {
            return null;
        }
        return JsonUtils.fromJsonAsMap(dbData, String.class, Object.class);
    }
}
